package fr.waitandsee.fragments;

import fr.waitandsee.FragmentController;
import fr.waitandsee.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class RecorderFragment extends Fragment implements PageFragmentModel {
	
	private static final String TAG = "RecorderFragment";
	
	private FragmentController fragmentController;
	private boolean hasAlreadyRecorded;
	
	private Button bSend;
	private Button bPlay;
	private Button bStop;
	private Button bRec;
	private TextView tvBack;
	
	public static RecorderFragment newInstance(/*parameters*/) {
		RecorderFragment fragment = new RecorderFragment();
		// set parameters if needed : 
	    // Bundle bundle = new Bundle(1);
	    // bundle.putInt(EXTRA_NAME, param);
	    // fragment.setArguments(bundle);
	    return fragment ;
	}
	
	
	  /////////////////////////
	 // FRAGMENT LIFE CYCLE //
	/////////////////////////
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate.savedInstanceState="+savedInstanceState);
		this.fragmentController = (FragmentController) getActivity();
		this.hasAlreadyRecorded = false;
	}
	
 	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 		Log.d(TAG, "onCreateView.savedInstanceState="+savedInstanceState);
		if (savedInstanceState != null) {
			// get saved parameters if needed : 
			// param = savedInstanceState.getInt(EXTRA_NAME);
		} else {
			if(getArguments() != null) {
				// get parameters if needed :
				// param = getArguments().getInt(EXTRA_NAME);
			}
		}
		View v = inflater.inflate(R.layout.fragment_recorder, container, false);
		return v;
	}
	
 	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
 		Log.d(TAG, "onViewCreated.savedInstanceState="+savedInstanceState);
		fetchViews();
		customizeViews();
		setListeners();
	}
 	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		Log.d(TAG, "onSaveInstanceState.outState="+outState);
		super.onSaveInstanceState(outState);
		// save parameters if needed :
		// outState.putInt(EXTRA_NAME, param);
	}
 	
	@Override
 	public void fetchViews() {
		this.bSend = (Button) this.getView().findViewById(R.id.button_send);
		this.bPlay = (Button) this.getView().findViewById(R.id.button_play);
		this.bStop = (Button) this.getView().findViewById(R.id.button_stop);
		this.bRec = (Button) this.getView().findViewById(R.id.button_rec);
		this.tvBack = (TextView) this.getView().findViewById(R.id.textView_back);
 	}
	
	@Override
	public void customizeViews() {
		
	}
 	
	@Override
	public void setListeners() {
		this.bSend.setOnClickListener(onSendClickListener);
		this.bPlay.setOnClickListener(onPlayClickListener);
		this.bStop.setOnClickListener(onStopClickListener);
		this.bRec.setOnClickListener(onRecClickListener);
		this.tvBack.setOnTouchListener(onBackTouchListener);
 	}
	
	
	  ///////////////
	 // LISTENERS //
	///////////////
	
 	private OnClickListener onSendClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Log.i(TAG, "onSendClickListener");
			//TODO action on "Send" button clicked
			fragmentController.switchContent(MediaEnrichmentFragment.newInstance(/*add parameters?*/));
		}
	};
	
 	private OnClickListener onPlayClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Log.i(TAG, "onPlayClickListener");
			//TODO action on "Play" button clicked
		}
	};
 	
 	private OnClickListener onStopClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Log.i(TAG, "onStopClickListener");
			//TODO action on "Stop" button clicked
			if(hasAlreadyRecorded) {
				bSend.setVisibility(View.VISIBLE);
				bSend.setClickable(true);
				bPlay.setVisibility(View.VISIBLE);
				bPlay.setClickable(true);
			}
		}
	};
	
 	private OnClickListener onRecClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Log.i(TAG, "onRecClickListener");
			//TODO action on "Rec" button clicked
			hasAlreadyRecorded = true;
		}
	};
	
	private OnTouchListener onBackTouchListener = new OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			Log.i(TAG, "onBackTouchListener");
			fragmentController.switchContent(SenderFragment.newInstance());
			return false;
		}
 	};
 	
}
